package com.wzdigit.wms.wms.service.across;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wzdigit.wms.basic.client.request.across.oms.OrderCloseInputDto;
import com.wzdigit.wms.basic.client.response.across.AcrossResult;
import com.wzdigit.wms.basic.domain.asn.AsnHeader;
import com.wzdigit.wms.basic.domain.transfer.GdnHeader;
import com.wzdigit.wms.basic.domain.transfer.TransferHeader;
import com.wzdigit.wms.basic.enums.*;
import com.wzdigit.wms.basic.enums.across.OrderCloseBillTypeEnum;
import com.wzdigit.wms.basic.enums.across.OrderCloseOperatTypeEnum;
import com.wzdigit.wms.basic.mapper.core.order.AsnDtlMapper;
import com.wzdigit.wms.basic.mapper.core.order.AsnHeaderMapper;
import com.wzdigit.wms.basic.mapper.core.transfer.GdnDtlMapper;
import com.wzdigit.wms.basic.mapper.core.transfer.GdnHeaderMapper;
import com.wzdigit.wms.basic.mapper.core.transfer.TransferDtlMapper;
import com.wzdigit.wms.basic.mapper.core.transfer.TransferHeaderMapper;
import com.wzdigit.wms.basic.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * 删改单控制-OMS删改单控制-服务类
 * @author yangdongjie
 * @GdnService.java
 * @2021年2月24日下午3:25:03
 */
@Service
public class OmsService {

	@Autowired
	private GdnHeaderMapper gdnHeaderMapper;
	
	@Autowired
	private GdnDtlMapper gdnDtlMapper;
	
	@Autowired
	private AsnHeaderMapper asnHeaderMapper;
	
	@Autowired
	private AsnDtlMapper asnDtlMapper;
	
	@Autowired
	private TransferHeaderMapper transferHeaderMapper;
	
	@Autowired
	private TransferDtlMapper transferDtlMapper;
	
	/**
	 * 删改单控制-OMS删改单控制
	 * @param dto
	 * @return
	 */
	@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
	public AcrossResult<OrderCloseInputDto> orderClose(OrderCloseInputDto dto) {
		// 校验单据类型和单据号是否空值
		if (!StringUtils.isNotNulls(dto.getBillType(), dto.getBillNo(), dto.getOperatType())) {
			AcrossResult<OrderCloseInputDto> acrossResult = new AcrossResult<>(
					false, "发生异常：单据类型、单据号和操作类型不允许为空！", dto);
			return acrossResult;
		}
		
		// 校验单据类型是否存在
		OrderCloseBillTypeEnum billTypeEnum = OrderCloseBillTypeEnum.getEnumByName(dto.getBillType());
		OrderCloseOperatTypeEnum operatTypeEnum = OrderCloseOperatTypeEnum.getEnumByName(dto.getOperatType());
		if (billTypeEnum == null || operatTypeEnum == null) {
			AcrossResult<OrderCloseInputDto> acrossResult = new AcrossResult<>(
					false, "发生异常：单据类型不存在或者操作类型错误！", dto);
			return acrossResult;
		}
		
		AcrossResult<OrderCloseInputDto> acrossResult = new AcrossResult<>(true, "允许删改单", dto);
		try {
			switch (billTypeEnum) {
			case GDN_TYPE:	// 发货通知单
				// 校验发货通知单是否存在
				QueryWrapper<GdnHeader> gdnHeaderQueryWrapper = new QueryWrapper<GdnHeader>();
				gdnHeaderQueryWrapper.eq("SOURCE_SHIP_NOTICE", dto.getBillNo());
				gdnHeaderQueryWrapper.eq("GDN_TYPE", "D01");
				if(gdnHeaderMapper.selectCount(gdnHeaderQueryWrapper) == 0) {
					acrossResult.setsucess(false);
					acrossResult.setMessage("发生异常：发货通知单不存在！");
					break;
				}
				
				// 校验发货通知单状态是否是【进行中】
				gdnHeaderQueryWrapper.eq("STATUS", GdnHeaderStatusEnum.DOING.code);
				if(gdnHeaderMapper.selectCount(gdnHeaderQueryWrapper) > 0) {
					acrossResult.setsucess(false);
					acrossResult.setMessage("不允许删改单，存在进行中的发货通知单");
					break;
				}
				
				/**
				 * 操作类型
				 */
				switch (operatTypeEnum) {
				case D:
					gdnDtlMapper.updateStatusBySourceShipNoticeAndGdnType(
							GdnDtlStatusEnum.CLOSE.code, dto.getBillNo(), "D01");
					gdnHeaderMapper.updateStatusBySourceShipNoticeAndGdnType(
							GdnHeaderStatusEnum.CLOSE.code, dto.getBillNo(), "D01");
					break;
				}
				break;
			case ASN_TYPE:	// 销售退货单
				// 校验销售退货单是否存在
				QueryWrapper<AsnHeader> asnHeaderQueryWrapper = new QueryWrapper<AsnHeader>();
				asnHeaderQueryWrapper.eq("ASN_NO", dto.getBillNo());
				asnHeaderQueryWrapper.eq("ASN_TYPE", "R04");
				if(asnHeaderMapper.selectCount(asnHeaderQueryWrapper) == 0) {
					acrossResult.setsucess(false);
					acrossResult.setMessage("发生异常：销售退货单不存在！");
					break;
				}
				
				// 校验销售退货单状态是否是【新建】
				asnHeaderQueryWrapper.eq("STATUS", AsnHeaderStatusEnum.CREATE.code);
				if(asnHeaderMapper.selectCount(asnHeaderQueryWrapper) == 0) {
					acrossResult.setsucess(false);
					acrossResult.setMessage("不允许删改单，不存在新建的销售退货单");
					break;
				}
				
				/**
				 * 操作类型
				 */
				switch (operatTypeEnum) {
				case D:	// 删除操作
					asnDtlMapper.updateStatusBySsnNoAndAsnTypeAndStatus(
							AsnDtlStatusEnum.FINISH.code, dto.getBillNo(),
							"R04", AsnHeaderStatusEnum.CREATE.code);
					asnHeaderMapper.updateStatusBySsnNoAndAsnTypeAndStatus(
							AsnHeaderStatusEnum.FINISH.code, dto.getBillNo(), 
							"R04", AsnHeaderStatusEnum.CREATE.code);
					break;
				}
				break;
			case TRANSFER_TYPE:	// 调拨单
				// 校验调拨单是否存在
				QueryWrapper<TransferHeader> transferHeaderQueryWrapper = new QueryWrapper<TransferHeader>();
				transferHeaderQueryWrapper.eq("TRANSFER_NO", dto.getBillNo());
				if(transferHeaderMapper.selectCount(transferHeaderQueryWrapper) == 0) {
					acrossResult.setsucess(false);
					acrossResult.setMessage("发生异常：调拨单不存在！");
					break;
				}
				
				// 校验调拨单状态是否是【新建】、【已审核】
				transferHeaderQueryWrapper.and(wrapper -> 
					wrapper.eq("STATUS", TransferHeaderStatusEnum.CREATE.code)
						.or().eq("STATUS", TransferHeaderStatusEnum.HASDONE.code));
				if(transferHeaderMapper.selectCount(transferHeaderQueryWrapper) == 0) {
					acrossResult.setsucess(false);
					acrossResult.setMessage("不允许删改单，不存在新建和已审核的调拨单");
					break;
				}
				
				/**
				 * 操作类型
				 */
				switch (operatTypeEnum) {
				case D:	// 删除操作
					transferDtlMapper.updateStatusByTransferNoAndStatus(
							TransferDtlStatusEnum.CLOSE.code, dto.getBillNo(),
							TransferHeaderStatusEnum.CREATE.code, TransferHeaderStatusEnum.HASDONE.code);
					transferHeaderMapper.updateStatusByTransferNoAndStatus(
							TransferHeaderStatusEnum.CLOSE.code, dto.getBillNo(),
							TransferHeaderStatusEnum.CREATE.code, TransferHeaderStatusEnum.HASDONE.code);
					acrossResult.setMessage("删除成功！");
					break;
				}
				break;
			}
		} catch (Exception e) {
			e.printStackTrace();
			acrossResult.setsucess(false);
			acrossResult.setMessage("发生异常：接口逻辑错误！");
		}
		return acrossResult;
    }
}
